package cn.iocoder.yudao.module.tms.service.order;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.tms.dal.dataobject.order.OrderRefImageDO;
import cn.iocoder.yudao.module.tms.dal.mysql.order.OrderRefImageMapper;

/**
 * 订单图片引用 Service 实现类
 *
 * @author 芋道源码
 */
@Service
public class OrderRefImageServiceImpl implements OrderRefImageService {

    @Resource
    private OrderRefImageMapper orderRefImageMapper;

    @Override
    public List<String> getImageUrlsByOrderId(Long orderId) {
        List<OrderRefImageDO> images = orderRefImageMapper.selectListByOrderId(orderId);
        return images.stream().map(OrderRefImageDO::getImageUrl).collect(Collectors.toList());
    }

    @Override
    public void saveOrderImages(Long orderId, List<String> imageUrls) {
        // 先删除原有的图片记录
        orderRefImageMapper.deleteByOrderId(orderId);

        // 如果有新的图片列表，则插入新记录
        if (CollUtil.isNotEmpty(imageUrls)) {
            for (int i = 0; i < imageUrls.size(); i++) {
                OrderRefImageDO imageDO = OrderRefImageDO.builder()
                        .orderId(orderId)
                        .imageUrl(imageUrls.get(i))
                        .sort(i)
                        .build();
                orderRefImageMapper.insert(imageDO);
            }
        }
    }

    @Override
    public void deleteOrderImagesByOrderId(Long orderId) {
        orderRefImageMapper.deleteByOrderId(orderId);
    }

}